package org.zoomdev.zoom.timer.impl;

import org.apache.commons.lang3.mutable.MutableInt;
import org.junit.Test;
import org.quartz.SchedulerException;
import org.zoomdev.zoom.common.utils.DateTimeUtils;
import org.zoomdev.zoom.timer.TimerJob;
import org.zoomdev.zoom.timer.TimerService;
import org.zoomdev.zoom.timer.modules.WrapAbandonExists;

import static org.junit.Assert.assertEquals;

public class QuartzTimerServiceTest2 {

    final Object lock = new Object();

    public static class TestLongJob implements TimerJob {

        private final Object data;
        private int count;

        public TestLongJob(Object data) {
            this.data = data;
        }


        @Override
        public void execute() throws Throwable {
            System.out.println("test job run " + DateTimeUtils.nowAsString());
            Thread.sleep((long) (Math.random() * 300));
            ++count;
            if (count >= 5) {
                synchronized (data) {
                    data.notify();
                }
            }
        }
    }

    @Test
    public void testResumeAndPause() throws SchedulerException, InterruptedException {
        QuartzTimerService service = new QuartzTimerService();

        MutableInt value = new MutableInt(0);
        System.out.println(DateTimeUtils.nowAsString());
        service.start("test", WrapAbandonExists.wrap(lock), "0/1 * * * * ? ");

        service.pause("test");

        Thread.sleep(3000);
        service.resume("test");
        synchronized (lock) {
            try {
                lock.wait();
                value.add(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        service.stop("test");
        assertEquals(value.intValue(), 1);

    }
}
